package com.kg.rails.ui.panels;

import com.kg.rails.component.editor.model.Cell;
import com.kg.rails.component.editor.model.custom.ElementCell;
import com.kg.rails.component.editor.model.custom.NodeCell;
import com.kg.rails.component.editor.model.custom.StationCell;
import com.kg.rails.component.editor.swing.GraphComponent;
import com.kg.rails.ui.panels.property.PropertyPanel;
import com.kg.rails.ui.util.UIHelper;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.Collection;

/**
 * User: Babanin
 * Date: 05.05.12
 * Time: 4:35
 */
public class InformationPanel extends JPanel {
    private GraphComponent component;
    private TitledBorder panelBorder;

    public InformationPanel(GraphComponent component) {
        this.component = component;
    }

    public void updateInformation() {
        removeAll();
        setLayout(new BorderLayout());

        panelBorder = BorderFactory.createTitledBorder(" Статистика ");
        setBorder(panelBorder);

        Object[] selectedCells = component.getGraph().getSelectionCells();
        int selectedCellCount = selectedCells.length;

        boolean showStatistics = true;

        if (selectedCellCount == 1) {
            PropertyPanel panel = PropertyPanel.getSuitablePanel((Cell) selectedCells[0], component);

            if (panel != null) {
                panelBorder.setTitle(" " + panel.getDescription() + " ");
                panel.build();

                add(panel, BorderLayout.CENTER);
                showStatistics = false;
            }
        }

        if (showStatistics) {
            Collection<Cell> cells = component.getGraph().getModel().getAllCells();

            int nodeCellCount = 0;
            int elementCellCount = 0;
            int stationCellCount = 0;

            for (Cell cell : cells) {
                if (cell instanceof ElementCell) {
                    elementCellCount++;
                } else if (cell instanceof NodeCell) {
                    nodeCellCount++;
                } else if (cell instanceof StationCell) {
                    stationCellCount++;
                }
            }

            add(UIHelper.createGridPanel(5, 5, 15, 5, 3, 2,
                    new JLabel("<html><b>Всего БУ:</b></html>"), new JLabel(String.valueOf(elementCellCount)),
                    new JLabel("<html><b>Всего стрелок:</b></html>"), new JLabel(String.valueOf(nodeCellCount)),
                    new JLabel("<html><b>Всего станций:</b></html>"), new JLabel(String.valueOf(stationCellCount)
            )), BorderLayout.CENTER);
        }
    }
}
